<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\exception\ValidateException;
use think\Request;

class User extends Base
{
    /**
     * 角色列表
     * api
     *
     * @auth kouweihong
     * @date 2020/11/24
     */
    public function index()
    {
        //获取参数
        $nickname = input('nickname');
        $field = input('field');
        $value = input('value');

        //查询
        $where = ['user_type' => \app\common\model\User::USER_TYPE_1];
        $nickname && $where['nickname'] = $nickname;
        $field && $value && $where[$field] = $value;
        $list = \app\common\model\User::where($where)->paginate($this->pageSize)->each(function ($v, $k) {
            $v->login_time = $v->login_time ? date('Y-m-d H:i:s', intval($v->login_time)) : '#';
        });
        $this->success($list);
    }

    /**
     * save #
     *
     * @auth kouweihong
     * @date 2020/12/4
     */
    public function save()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $scene = isset($params['id']) ? 'edit' : '';
            validate(\app\common\validate\User::class)->scene($scene)->check($params);

            //保存数据
            isset($params['password']) && $params['password'] = \app\common\model\User::encryptPassword($params['password']);
            $user = isset($params['id']) ? \app\common\model\User::update($params, ['id' => $params['id']]) : \app\common\model\User::create($params);
            (!$user || !$user->id) && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取学生列表
     * api
     *
     * @auth kouweihong
     * @date 2020/11/26
     */
    public function studentList()
    {
        //获取参数
        $nickname = input('nickname');
        $field = input('field');
        $value = input('value');

        //查询
        $where = ['user_type' => \app\common\model\User::USER_TYPE_10];
        $nickname && $where['nickname'] = $nickname;
        $field && $value && $where[$field] = $value;
        $list = \app\common\model\User::with(['user10'])->where($where)->paginate($this->pageSize)->each(function ($v, $k) {
            $v->login_time = $v->login_time ? date('Y-m-d H:i:s', intval($v->login_time)) : '#';
        });
        $this->success($list);
    }

    /**
     * 添加学生
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/11/27
     */
    public function addStudent()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $scene = isset($params['id']) ? 'edit' : '';
            validate(\app\common\validate\User::class)->scene($scene)->check($params);

            //保存数据
            isset($params['password']) && $params['password'] = \app\common\model\User::encryptPassword($params['password']);
            $user = isset($params['id']) ? \app\common\model\User::update($params, ['id' => $params['id']]) : \app\common\model\User::create($params);
            (!$user || !$user->id) && $this->error('操作失败');

            //保存扩展参数
            $res = $user->user10->save([$params]);
            $res === false && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * updatePwd #修改密码
     * api post /{controller}/{method}
     * @param string pwd 旧密码
     * @param string npwd 新密码
     * @param string cnpwd 重复新密码
     *
     * @auth kouweihong
     * @date 2020/12/4
     */
    public function updatePwd()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $params['password'] = $this->auth->password;
            $params['pwd'] = \app\common\model\User::encryptPassword($params['pwd']);
            validate(['pwd|旧密码' => 'require|confirm:password', 'npwd|新密码' => 'require', 'cnpwd|重复新密码' => 'require|confirm:npwd'])->check($params);

            //保存
            $res = $this->auth->save(['password' => \app\common\model\User::encryptPassword($params['npwd'])]);
            $res === false && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 更新用户状态
     * api post /{controller}/{method}
     *
     * @return int code 状态码
     * @return string msg 消息
     * @return array data 数据
     * @auth kouweihong
     * @date 2020/11/24
     */
    public function updateStatus()
    {
        $this->request->isPost() || $this->error('非法操作');
        $id = input('post.id');
        $status = input('post.status');
        (!$id || !in_array($status, [\app\common\model\User::STATUS_0, \app\common\model\User::STATUS_1])) && $this->error('参数错误');

        //查询用户信息
        $user = \app\common\model\User::getById($id);
        $user || $this->error('信息记录不存在');

        //更新状态
        $res = $user->save(['status' => $status]);
        $res === false && $this->error('操作失败');

        $this->success();
    }

    /**
     * del #
     *
     * @param $id
     * @auth kouweihong
     * @date 2020/11/26
     */
    public function del($id)
    {
        //获取参数
        $id || $id = input('id');
        $id || $this->error('参数错误');
        $id == 1 && $this->error('此用户不可删除');

        //查询信息
        $user = \app\common\model\User::find($id);
        $user || $this->error('信息记录不存在');
        $user->status == \app\common\model\User::STATUS_F_1 && $this->error('此用户已删除');

        //删除
        $res = $user->save(['status' => \app\common\model\User::STATUS_F_1]);
        $res === false && $this->error('操作失败');

        $this->success();
    }
}
